from bilibili_api import comment

from bilibili_api import Credential

from bilibili_api import video
from bilibili_api import sync

from file_operate import func_write_to_json as json_write
from file_operate import func_write_file as file_write
from typing import List
from bilibili_api import Danmaku
from bilibili_api.user import User
from get_time import func_from_timestamp_to_formate_time
import xmltodict


class Video:
    def __init__(self, credential: Credential, bv: str):
        self.my_bv = bv
        self.my_credential = credential
        self.my_video = video.Video(bvid=self.my_bv, credential=self.my_credential)

    def method_get_comment(self):
        # 获取av号
        lv_av = video.Video(bvid=self.my_bv, credential=self.my_credential).get_aid()
        lv_page = 1
        lv_comments = []
        lv_comment_count = 0
        while True:
            lv_comment: dict = sync(comment.get_comments(oid=lv_av,
                                                         page_index=lv_page,
                                                         credential=self.my_credential,
                                                         order=comment.OrderType.TIME,
                                                         type_=comment.CommentResourceType.VIDEO))
            lv_comments += lv_comment['replies']
            lv_page += 1
            lv_comment_count = lv_comment['page']['size']
            if lv_comment_count >= lv_comment['page']['count']:
                break
        print(lv_comments)
        lv_text = ''
        for val in lv_comments:
            uname = val['member']['uname']
            msg = val['content']['message']
            lv_text += f'{uname}: "{msg}"\n'
        file_write('./all_comments.txt', lv_text)
        pass

    def method_get_danmaku(self):
        lv_page = 0
        # 获取弹幕。
        lv_info_danmakus: List = sync(self.my_video.get_danmakus(page_index=lv_page))
        # 获取所有弹幕的 xml 源文件（非装填）
        lv_info_danmaku_xml: str = sync(self.my_video.get_danmaku_xml(page_index=lv_page))
        lv_content = ''
        count = 0
        for danmaku in lv_info_danmakus:
            break
            count += 1
            if count >= 15:
                break
            danmaku: Danmaku
            text = danmaku.text
            crc32_id = danmaku.crc32_id
            dm_time, send_time = danmaku.dm_time, danmaku.send_time
            uid = danmaku.crack_uid(crc32_id)
            # print(uid)
            send_time_str: str = func_from_timestamp_to_formate_time(send_time)
            uname = ''
            try:
                uname = sync(User(uid=uid).get_user_info_sync())['name']

            except:
                uname = 'not found'
            lv_content += f'{send_time_str} {uname} : {text} {dm_time}s\n'
        print(lv_content)
        # file_write(file_name='./danmaku.txt', content=lv_content)
        # 把xml转成dict
        lv_info_xml_dict: dict = xmltodict.parse(xml_input=lv_info_danmaku_xml)
        print(lv_info_xml_dict)
        # json_write(json_file_name='./danmaku_xml_to_json.json', json_dict=lv_info_xml_dict)
        for val in lv_info_xml_dict['i']['d']:
            text = val['#text']
            s: str = val['@p']
            s_list = s.split(',')
            dm_time, send_time = s_list[0], s_list[4]
            print(text, dm_time, func_from_timestamp_to_formate_time(float(send_time)))

    def method_loop_reply_comment(self):
        pass
